<?php
//+-----------------------------------------------------------
//| 人生是荒芜的旅行，冷暖自知，苦乐在心
//+-----------------------------------------------------------
//| Author:Janmas <janmas@126.com>
//+-----------------------------------------------------------
//|
//+-----------------------------------------------------------


namespace app\common\plugins;


use app\admin\exception\LogicException;

/**
 * 插件基础类
 * @package app\common\plugins
 */
abstract class Plugin
{
    /**
     * 插件标识
     * @var string
     */
    protected $flag = '';

    /**
     * 插件地址
     * @var string
     */
    protected $pluginPath = '';

    /**
     * 插件配置信息
     * @var array
     */
    protected $info = [];

    /**
     * 插件配置信息
     * @var array
     */
    protected $config = [];

	/**
	 * 插件是否安装
	 * @var bool
	 */
    protected $installed = false;

    public function __construct ()
    {
        $this->flag       = $this->getFlag();
        $this->pluginPath = $this->getPluginPath() . $this->flag . DIRECTORY_SEPARATOR;
        $this->installed  = $this->checkInstalled();
    }

    /**
     * 插件名称
     * @return string
     */
    abstract public function getName ();

    /**
     * 插件标识
     * @return string
     */
    abstract public function getFlag ();

    /**
     * 首页默认url
     * @return string
     */
    abstract public function getHomeAction();
    /**
     * 安装
     * @return mixed
     */
    public function install ()
    {
        return true;
    }

    /**
     * 卸载
     */
    public function uninstall ()
    {
        return true;
    }

    /**
     * 升级
     */
    public function upgrade ()
    {
        return true;
    }

    /**
     * 读取插件信息
     * @return array|mixed
     */
    final public function getInfo ( String $name = '',$default='' )
    {

        $infoFile = $this->pluginPath . 'info.json';
        if ( !is_file($infoFile) ) {
            throw new LogicException(sprintf('插件%s的info.json文件不存在' , $this->flag));
        }
        $info = json_decode(file_get_contents($infoFile) , true);
        return is_null($info) ? [] : $info;
    }

    /**
     * 读取配置信息
     * @return array|false
     */
    final public function getConfig ( $name = '' )
    {
        if ( $this->installed ) {
            if ( empty($name) ) {
                return config($this->flag);
            }
            return config($this->flag . '.' . $name);
        }

        $configFile = $this->pluginPath . 'config.json';
        if ( !is_file($configFile) ) {
            return [];
        }
        $config = json_decode(file_get_contents($configFile) , true);
        return is_null($config) ? [] : $config;
    }

    /**
     * 读取菜单信息
     * @return array|mixed
     */
    final public function getMenus ()
    {
        $menuFile = $this->pluginPath . 'menu.json';
        if ( !is_file($menuFile) ) {
            return [];
        }

        $menus = json_decode(file_get_contents($menuFile) , true) ?? [];

        return $menus;
    }

    /**
     * 获取插件地址
     * @return string
     */
    final public function getPluginPath ()
    {
        return app()->getRootPath() . 'plugins' . DIRECTORY_SEPARATOR;
    }

    /**
     * 检查插件是否安装
     * @return bool
     */
    final protected function checkInstalled ()
    {
        if ( is_file($this->pluginPath . 'install.lock') ) {
            return true;
        }
        return false;
    }

	/**
	 * @return bool
	 */
    final public function getInstalled(){
    	return $this->installed;
    }

    public function __call ( $name , $arg )
    {
        if ( method_exists($this , $name) ) {
            return $this->$name($arg);
        }
    }
}